package homework.jdbc.Utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/13.
 */
public class JDBCUtils {
    //连接池
    public static String configName="usertest";
    //C3P0的数据源对象                                                   //数据源对象名称，不写或写错，运行默认配置
    private static ComboPooledDataSource cpds = new ComboPooledDataSource(configName);

    public static Connection getConnection(){
        try {
            return cpds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static DataSource getDataSource(){
        return cpds;
    }

    public static String getConfigName() {
        return configName;
    }

    public static void setConfigName(String configName) {
        JDBCUtils.configName = configName;
    }

    //    private static Connection conn = null;
//    public static Connection getConnection() {
//        try {
//        Class.forName("com.mysql.jdbc.Driver");
//        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/usertest","root","root");
//        return conn;
//    } catch (Exception e) {
//        e.printStackTrace();
//    }
//        return null;
//}
//    public static void Relase(Statement statement){
//        if (conn!=null){
//            try {
//                conn.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//        if (statement!=null){
//            try {
//                statement.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//    }

}
